package models.production;

import com.avaje.ebean.Ebean;
import com.avaje.ebean.Query;
import com.avaje.ebean.RawSql;
import com.avaje.ebean.RawSqlBuilder;
import models.Product;
import models.core.GipxyModel;
import models.core.StatBean;
import play.db.ebean.Model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.validation.Constraint;
import java.text.DecimalFormat;
import java.text.NumberFormat;

/**
 * Created by Gipxy group
 * Date: 5/1/12
 */
@Entity
public class ProdRequest extends GipxyModel {
    public Long soLenhSx;

    public Product product;

    public Integer soLuongTrienKhai;
    
    
    public String getSoLenhSxString() {
        NumberFormat formatter = new DecimalFormat("000000");
        return formatter.format(soLenhSx);
    }

    public void setSoLenhSx(Long soLenhSx) {
        this.soLenhSx = soLenhSx;
    }

    public static Finder<Long,ProdRequest> find = new Finder<Long,ProdRequest>(Long.class, ProdRequest.class);

    public static long getNextSoLenhSX() {
        RawSql rawSql = RawSqlBuilder.parse("select max(so_lenh_sx) as tmpNumber from prod_request").create();

        Query<StatBean> query = Ebean.find(StatBean.class);

        //Finder<Long,StatBean> tmp = new Finder<Long,StatBean>(Long.class, StatBean.class);
        query.setRawSql(rawSql);

        return query.findUnique().tmpNumber==null? 0 : query.findUnique().tmpNumber;
    }
}
